<?php
namespace Admin\Controller;
use Think\Controller;


class CompanyController extends BaseController {
	public function index(){
		
	$data=M('company')->select();
	$this->assign("info",$data);
	 $this->display();
	}
	
 /* 公司添加 */
    public function add()
    {

		
        if( IS_GET ){ //访问页面
            $this->display();
        }else{ //表单提交
            $company = M('company');
            $data['name']=$_POST['name'];
             $data['pwd']=md5("123456");
            $data['description']=$_POST['description'];
         if ($_FILES ['pic'] ['name'] != null)
         		$data ["pic"] = $this->upload ( $_FILES ['pic'] );
         		
            if( $company->create() ){
                $state = $company->add($data);

                if( $state===false ){
                    $this->error('公司添加失败，错误信息：'.$company->getDbError());
                }else{
                	
                	
                    $this->success('公司添加成功', U('company/index'), 0);
                }
            }else{ //表单提交不完整
                $this->assign(I('post.')); //反馈提交的数据
                $this->assign('errorNote', $company->getError());
                $this->display();
            }
        }
    }
    
   
    
    /**
     * 公司优惠券列表
     */
    public function couponlist(){
    	$company_id=I("get.id");
    	$data=M("coupon")->where("company_id=".$company_id)->select();
    	$this->assign("company_id",$company_id);
    	$this->assign("info",$data);
    	$this->display();
    
    }
    
    /**
     * 增加优惠券
     */
    public function couponadd(){
    	if(IS_POST){
    		$info=M("coupon");
    		
    		$num=I("post.num");
    		$id=I("post.id");
    		for($i=0;$i<$num;$i++){
    			$string=randomkeys(12);
    			while ($info->where("value='".$string."'")->count()){
    			$string=randomkeys(12);
    		
    			}
    		$data=array(
    		"company_id"=>$id,
    		"value"=>$string,
    		"btime"=>strtotime($_POST['btime']." 00:00:00"),
    		"etime"=>strtotime($_POST['etime']." 00:00:00"),
    		"isused"=>0,
    		"context"=>$_POST['context']
    		);
    		$state=$info->add($data);
    		}
    		
    		
    		if( $state===false ){
                    $this->error('优惠券添加失败，错误信息：');
                }else{
                    $this->success('优惠券添加成功', U('company/couponlist',array('id'=>$id)), 0);
                }
    	}
    	else {
    	$this->assign("company_id",I("get.id"));
    	$this->display();
    	}
    
    }
    

    /* 优惠券删除 */
    public function coupondelete()
    {
    	$company_id=I('get.company_id')==null?I('post.company_id'):I('get.company_id');
        if( is_array(I('id')) ){ //批量删除
            $id = implode( ',', I('id', array(), 'intval') );
        }else{ //单个删除
            $id = I('id/d');
        }

        $coupon = D('coupon');
	$state = $coupon->delete($id);
        if( $state===false ){
            $this->error('公司删除失败，错误信息: '.$coupon->getDbError()); //删除问卷，并关联删除旗下问题
        }else{
        	//var_dump(I('get.company_id'));
            redirect( U('company/couponlist',array('id'=>$company_id)));
        }
    }

    /* 公司编辑 */
    public function edit()
    {

        if( IS_GET ){ //访问页面
            $company = M('company')->field(true)->find( I('get.id/d') );
            $this->assign($company);
            $this->display();
        }else{ //表单提交
            $company = M('company');
            $data['id']=$_POST['id'];
            $data['name']=$_POST['name'];
            $data['description']=$_POST['description'];
         if ($_FILES ['pic'] ['name'] != null)
         		$data ["pic"] = $this->upload ( $_FILES ['pic'] );

            if( $company->create() ){ //表单提交
                $state = $company->save($data);

                if( $state===false ){
                    $this->error('问卷更新失败，错误信息：'.$company->getDbError());
                }else{
                    $this->success('问卷编辑成功', U('company/index'), 0);
                }
            }else{ //表单提交不完整
                $this->assign(I('post.')); //反馈提交的数据
                $this->assign('errorNote', $company->getError());
                $this->display();
            }
        }
    }

    /* 公司删除 */
    public function delete()
    {
        if( is_array(I('id')) ){ //批量删除
            $id = implode( ',', I('id', array(), 'intval') );
        }else{ //单个删除
            $id = I('id/d');
        }

        $company = D('company');
	$state = $company->relation(true)->delete($id);
        if( $state===false ){
            $this->error('公司删除失败，错误信息: '.$company->getDbError()); //删除问卷，并关联删除旗下问题
        }else{
            redirect( U('company/index'));
        }
    }
    
/**
	 * 上传
	 * @param unknown_type $filename
	 * @return string
	 */
	public function upload($filename) {
		$upload = new \Think\Upload (); // 实例化上传类
		$upload->maxSize = 3145728; // 设置附件上传大小
		$upload->exts = array ('jpg', 'gif', 'png', 'jpeg' ); // 设置附件上传类型
		$upload->rootPath = './Public/Upload/'; // 设置附件上传根目录
		$upload->savePath = '';
		// 上传单个文件 
		$info = $upload->uploadOne ( $filename );
		if (! $info) { // 上传错误提示错误信息
			$this->error ( $upload->getError () );
			exit ();
		} else { // 上传成功 获取上传文件信息
			return $info ['savepath'] . $info ['savename'];
		}
	}
	
}